package com.hzh.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzh.entity.OperationResult;
import com.hzh.entity.UserEntity;
import com.hzh.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
    @Autowired
    private UserService userService;
    
	@RequestMapping("/login")
    @ResponseBody
    public OperationResult login(UserEntity userEntity,HttpServletRequest request) throws Exception{
    	//获取session对象用于拦截器对已登录用户进行判断
	    HttpSession session = request.getSession();
    	OperationResult result = new OperationResult();
    	//通过用户名和密码获取用户
        UserEntity user = userService.login(userEntity);
        //判断用户是否存在
    	if(user!=null){
    	    //用户存在则进行登录并设置session
    		session.setAttribute("user", user);
    		//获取用户ID用来在FileController中获取用户角色和用户名
    		result.setUserId(user.getUserId());
        	return result;
        }else{
	        result.setSuccess(false);
	        return result;
        }
    }
    
    @RequestMapping(value="/logout")  
    public String logout(HttpSession session) throws Exception{  
        //点击退出按钮清除Session  
        session.invalidate();
        //返回登录页面
        return "login";  
    } 
}
